package com.dky.satell.controller;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.dky.satell.dto.ResultDto;
import com.dky.satell.entity.OfflineFile;
import com.dky.satell.scheduled.FileLoad;
import com.dky.satell.scheduled.FileScan;
import com.dky.satell.service.OfflineFileService;
import com.dky.satell.thirdParty.label.LabelTemplate;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mqc
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/gather/offlineFile")
@Slf4j
public class OfflineFileRestController {

    @Resource
    private OfflineFileService offlineFileService;

    @Resource
    private FileScan fileScan;

    @Resource
    private FileLoad fileLoad;

    @Value("${file.uploadPath}")
    private String uploadPath;

    @Autowired
    private LabelTemplate labelTemplate;


    /**
     * 查询文件列表
     */
    @RequestMapping("/findOfflineFile")
    public ResultDto findFile() {
        List<OfflineFile> list = offlineFileService.list();
        return ResultDto.success("成功！", list);
    }

    /**
     * 模糊查询文件列表
     */
    @PostMapping("/findFileByFileName")
    public ResultDto findFileByFileName(@RequestBody Map<String, Object> maps) {
        Integer pageNo = (Integer) maps.get("pageNo");
        Integer pageSize = (Integer) maps.get("pageSize");
        String name = (String)maps.get("name");
        Page<OfflineFile> fileByFileName = offlineFileService.findFileByFileName(pageNo,pageSize,name);
        HashMap<String, Object> fileMap = new HashMap<>();
        fileMap.put("total", fileByFileName.getTotal());
        fileMap.put("pageNo", fileByFileName.getPageNum());
        fileMap.put("pages", fileByFileName.getPages());
        fileMap.put("pageSize", fileByFileName.getPageSize());
        fileMap.put("rows", fileByFileName.getResult());
        return ResultDto.success("", fileMap);
    }

    /**
     * 根据id查看文件详情
     */
    @PostMapping("/selectOffLineFileById")
    public ResultDto selectOffLineFileById(@RequestBody Map<String, Integer> maps){
        Integer id = maps.get("id");
        List<OfflineFile> offlineFiles = offlineFileService.selectOffLineFileById(id);
        return ResultDto.success("成功！", offlineFiles);
    }

    /**
     * 删除
     */
    @PostMapping("/deleteOfflineFileById")
    public ResultDto deleteOfflineFileById(@RequestBody HashMap<String,Integer> map){
        Integer id = map.get("id");
        boolean b = offlineFileService.removeById(id);
        if(b){
            return ResultDto.success("删除成功！",map.get("id"));
        }else{
            return ResultDto.fail("删除成功！",map.get("id"));
        }
    }

    /**
     * 处理文件上传
     */
    @PostMapping(value = "/upload")
    public ResultDto uploading(@RequestParam("file") MultipartFile file, OfflineFile offlineFile) {
        log.info("begin to /offlineFile/upload/[{}]", offlineFile.getFileName());
        ResultDto resultDto = offlineFileService.insert(file, offlineFile);
        log.info("end to /offlineFile/upload");
        return resultDto;
    }

    /**
     * 生成xml文件
     */
    @PostMapping(value = "/generateXml")
    public ResultDto generateXml(@RequestBody JSONObject body) {
        String fileName =  body.getString("fileName");
        log.info("begin to /offlineFile/generateXml/{}", fileName);
        try {
            File xmlFile = Files.createFile(Paths.get(uploadPath + fileName + ".xml")).toFile();
            FileUtil.writeBytes(body.getBytes("content"), xmlFile);
            log.info("xml文件[{}]生成成功", fileName + ".xml");
        } catch (IOException e) {
            log.error("xml文件[{}]生成失败", fileName + ".xml");
        }
        log.info("end to /offlineFile/generateXml");
        return ResultDto.success();
    }

    /**
     * 处理自动上传
     */
    @GetMapping(value = "/autoScan")
    public ResultDto autoScan() {
        log.info("begin to /offlineFile/autoScan");
        fileScan.scheduled();
        log.info("end to /offlineFile/autoScan");
        return ResultDto.success();
    }

    /**
     * 处理自动加载
     */
    @GetMapping(value = "/autoLoad")
    public ResultDto autoLoad() {
        log.info("begin to /offlineFile/autoLoad");
        fileLoad.scheduled();
        log.info("end to /offlineFile/autoLoad");
        return ResultDto.success();
    }

    /**
     * 登出30s标签系统
     */
    @GetMapping(value = "/logout")
    public ResultDto logout() {
        log.info("begin to /offlineFile/logout");
        labelTemplate.logout();
        log.info("end to /offlineFile/logout");
        return ResultDto.success();
    }

}
